package com.offcn.entity;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.io.Serializable;

public class User implements Serializable, HttpSessionBindingListener {
    private int id;
    private String username;
    private String password;
    private String email;
    private String phone;



    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        //登陆成功时会在session里绑定一个user对象，然后调用该方法，可以再这里做一个统计用户数的操作
        ServletContext servletContext = event.getSession().getServletContext();
        Object userAccount = servletContext.getAttribute("UserAccount");
        if(userAccount==null){
            //为空说明在此之前没有用户登陆过，就把当前用户设置为第一个
            servletContext.setAttribute("UserAccount",1);
        }else{
            //在之前用户数的基础上加1
            int count = (int) userAccount + 1;
            servletContext.setAttribute("UserAccount",count);
        }
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        //用户退出时，会销毁session,随机调用该方法，在此统计在线用户数
        ServletContext servletContext = event.getSession().getServletContext();
        Object userAccount = servletContext.getAttribute("UserAccount");
        if(userAccount!=null){
            if((int)userAccount>0){
                //说明还有用户登录着的，直接在当前用户数上-1
                int count = (int) userAccount - 1;
                servletContext.setAttribute("UserAccount",count);
            }else{
                //此时没有用户登录，防止获取到的用户数<=0,直接将userAccount的值设为0
                servletContext.setAttribute("UserAccount",0);
            }

        }
    }

    public User() {
    }

    public User(int id, String username, String password, String email) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public User(int id, String username, String password, String email, String phone) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.phone = phone;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}
